import apiUrl from "./dev.js";
const baseURL = apiUrl.HTML_SERVE;

function request(params = {}) {
	// 请求方式 默认为 GET请求
	params.method = params.method ? params.method : "GET";

	// isJsonType	是否使用json格式传请求参数，true 或者 false，缺省 false。
	if (params.isJsonType == null && params.method.toUpperCase() == "POST") {
		params.isJsonType = true;
	}

	// 数据请求格式默认为表单
	let contentType = "application/x-www-form-urlencoded";

	if (params.isJsonType && params.method.toUpperCase() == "POST") {
		params.data = JSON.stringify(filterNull(params.data)); // 当请求为post时 需要序列化 JSON.stringify
		contentType = "application/json; charset=utf-8";
	}

	return new Promise((resolve, reject) => {
		$.ajax({
			url: baseURL + params.url || "",
			timeout: 15000,
			type: params.method,
			data: params.data || {},
			contentType: contentType,
			beforeSend: (xhr) => {
				// xhr.setRequestHeader();
			},
			success: (data) => {
				resolve(data);
			},
			error: (error) => {
				reject(error);
			}
		});
	});
}

function filterNull(data) {
	for (const i in data) {
		if (typeof data[i] == "undefined" || data[i] == null) {
			delete data[i];
		}
	}
	return data;
}

export function getInformationList(data) {
	return request({
		url: "/wljy/mp/manage/information/getInformationList",
		method: "POST",
		data
	});
}
export function dictTree(data) {
	return request({
		url: `/wljypermission/admin/dict/${data}/tree`,
		method: "GET",
	});
}
//资讯详情
export function details(data) {
	return request({
		url: `/wljy/mp/manage/information/details/${data}`,
		method: "GET"
	});
}

export function getParkPhotoList(data) {
	return request({
		url: "/wljy/mp/manage/banner/pageList",
		method: "POST",
		data
	});
}

export function getParkVideoList(data) {
	return request({
		url: "/wljy/mp/manage/videoManage/list",
		method: "POST",
		data
	});
}

export function checkCode(code, codeKey) {
	return request({
		url: `/wljy/mp/manage/suggest/checkCode?code=${code}&codeKey=${codeKey}`,
		method: "get",
	});
}

export function add(data) {
	return request({
		url: `/wljy/mp/manage/suggest/add`,
		method: "post",
		data
	});
}